package cn.iocoder.yudao.module.ent.controller.admin.usertemplate;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.ent.controller.admin.usertemplate.vo.UserTemplatePageReqVO;
import cn.iocoder.yudao.module.ent.controller.admin.usertemplate.vo.UserTemplateRespVO;
import cn.iocoder.yudao.module.ent.controller.admin.usertemplate.vo.UserTemplateSaveReqVO;
import cn.iocoder.yudao.module.ent.dal.dataobject.usertemplate.UserTemplateDO;
import cn.iocoder.yudao.module.ent.service.usertemplate.UserTemplateService;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "企业大数据 - 用户查询模板")
@RestController
@RequestMapping("/ent/user-template")
@Validated
public class UserTemplateController {

    @Resource
    private UserTemplateService userTemplateService;


    @Resource
    private AdminUserApi adminUserApi;

    @PostMapping("/create")
    @Operation(summary = "创建用户查询模板")
    public CommonResult<Long> createUserTemplate(@Valid @RequestBody UserTemplateSaveReqVO createReqVO) {
        return success(userTemplateService.createUserTemplate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户查询模板")
    public CommonResult<Boolean> updateUserTemplate(@Valid @RequestBody UserTemplateSaveReqVO updateReqVO) {
        userTemplateService.updateUserTemplate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户查询模板")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteUserTemplate(@RequestParam("id") Long id) {
        userTemplateService.deleteUserTemplate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户查询模板")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<UserTemplateRespVO> getUserTemplate(@RequestParam("id") Long id) {
        UserTemplateDO userTemplate = userTemplateService.getUserTemplate(id);
        return success(BeanUtils.toBean(userTemplate, UserTemplateRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得用户查询模板列表")
    public CommonResult<List<UserTemplateRespVO>> getUserTemplateList() {
        List<UserTemplateDO> userTemplate = userTemplateService.getUserTemplateList();

//        List<AdminUserRespDTO> userList = adminUserApi.getUserList();
//        Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(convertList(userList, AdminUserRespDTO::getId));

        return success(BeanUtils.toBean(userTemplate, UserTemplateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户查询模板分页")
    public CommonResult<PageResult<UserTemplateRespVO>> getUserTemplatePage(@Valid UserTemplatePageReqVO pageReqVO) {
        PageResult<UserTemplateDO> pageResult = userTemplateService.getUserTemplatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserTemplateRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户查询模板 Excel")
    @PreAuthorize("@ss.hasPermission('ent:user-template:export')")
    @OperateLog(type = EXPORT)
    public void exportUserTemplateExcel(@Valid UserTemplatePageReqVO pageReqVO,
                                        HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserTemplateDO> list = userTemplateService.getUserTemplatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户查询模板.xls", "数据", UserTemplateRespVO.class,
                BeanUtils.toBean(list, UserTemplateRespVO.class));
    }

}